(function($) {
	//jquery原型添加pager方法
	$.fn.pager = function(options) {
			var opts = $.extend(true,{}, $.fn.pager.defaults, options);  //第一个参数为true深度copy
			$.fn.initParam(opts);   
			$this = $(this);
			$this.hide();       //隐藏分页栏
			$this.empty();
	};
	
	$.fn.initParam = function (opts){ 
		$.ajax({ 
			url:opts.url,
			type:"post",
			data:opts.data, 
			dataType:"json",
			error: function(){alert('请求超时，请稍候再试');},
			success: function(result){  
					if(result&&result.rows.length>0){
						opts.onResult(result.rows);
						var allPages = Math.ceil(result.total/opts.data.rows);  //总共有多少页
						var o = $.extend({},opts,{allPages:allPages},{total:result.total}); 
						$.fn.draw(o,o.data.page);
					}else{
						opts.onResult(null);
					} 
			} 
		});
	};
	
	$.fn.pager.defaults = {
		total       : 0,                         //共多少记录
		display  	: 5,                         //默认显示多少页数
		showDetail  : false,                     //是否显示详细信息 
		url			: '',                        //后台通信url
		data        : {page:1,rows:10},          //page 默认显示第几页  rows 默认每页显示多少条数据
		onResult    : function(){return false;}, //回调函数返回结果
		onChange	: function(){return false;}
	};
	
	$.fn.draw = function(o,selectedpage,flag){
		$this.show();           //显示该分页栏
		if(o.display > o.allPages)
			o.display = o.allPages;
		var _pageDiv = $(document.createElement('div')).addClass("pages");
		var _first = $(document.createElement('a')).attr("mark","shouye").html('首页');       //首页dom
		_pageDiv.append(_first);
		if(flag=="1"){
			for(var i = 0; i < o.display; i++){                           
				var val = parseInt(i)+parseInt(selectedpage)-4;
				if(val == selectedpage){
					var _obj = $(document.createElement('a')).addClass("curPage").html(val);
					_pageDiv.append(_obj);
				}	
				else{
					var _obj = $(document.createElement('a')).html(val);
					_pageDiv.append(_obj);
					}				
			}
		}else if(flag=="2"){
			for(var i = 0; i < o.display; i++){                            
				var val = parseInt(i)+parseInt(o.allPages)-7;
				if(val == selectedpage){
					var _obj = $(document.createElement('a')).addClass("curPage").html(val);
					_pageDiv.append(_obj);
				}	
				else{
					var _obj = $(document.createElement('a')).html(val);
					_pageDiv.append(_obj);
					}				
			}
		}else{
			for(var i = 0; i < o.display; i++){                            
				var val = i+1;
				if(val == selectedpage){
					var _obj = $(document.createElement('a')).addClass("curPage").html(val);
					_pageDiv.append(_obj);
				}	
				else{
					var _obj = $(document.createElement('a')).html(val);
					_pageDiv.append(_obj);
					}				
			}
		}
		
		if(parseInt(o.allPages)!=parseInt(selectedpage)){
			var _next = $(document.createElement('a')).attr("mark","next").html('下一页');       //下一页dom
			_pageDiv.append(_next);
		}	
		var _last = $(document.createElement('a')).attr("mark","moye").html('末页');       //末页dom
		_pageDiv.append(_last);
		 
		var _label = $(document.createElement('label')).html("跳转&nbsp;<input class='jumpInput' id='jumpInput'/>&nbsp;页&nbsp;");       //label
		_pageDiv.append(_label);
		
		var _button = $(document.createElement('input')).attr("type","button").attr("value","确定").addClass("sureBtn");       //button
		_pageDiv.append(_button);
		
		var _clearDiv = $(document.createElement('div')).addClass("clearDiv");
		_pageDiv.append(_clearDiv); 
		
		if(o.showDetail){
			var _pageDetail = $(document.createElement('div')).addClass("pageDetail");
			_pageDetail.html(o.total+"条记录，共"+o.allPages+"页"); 
			$this.append(_pageDetail); 
		}
		$this.append(_pageDiv);
		 
		$("input[class='jumpInput']").keydown(function(e){
			//return event.keyCode>=48&&event.keyCode<=57||(this.value.indexOf('.')<0?event.keyCode==46:false);
			if ($.browser.msie) {  // 判断浏览器
                if ( ((event.keyCode > 47) && (event.keyCode < 58)) || (event.keyCode == 8) ) { // 判断键值  
                       return true;  
                 } else {  
                       return false;  
                }
          } else {  
             if ( ((e.which > 47) && (e.which < 58)) || (e.which == 8) || (event.keyCode == 17) ) {  
                      return true;  
              } else {  
                      return false;  
              }  
          }}).focus(function() {
                  this.style.imeMode='disabled';   // 禁用输入法,禁止输入中文字符
          });
		     
		_button.click(function(){
			var currval = parseInt($.trim($("#jumpInput").val()));  
			if(isNaN(currval)||currval==0){       //如果输入的为空或者为非数字或者0    
				$("#jumpInput").val(""); 
				return ;
			}else{ 
				if($.trim($("#jumpInput").val()).substr(0,1)=="0"){         //判断首字母是否为0 
					$("#jumpInput").val(""); 
					return ;
				} 
				if(currval>parseInt(o.allPages)){                          //用于判断当前页数是否大于总页数
					$("#jumpInput").val(""); 
					return;
				} 
				$this.empty();
				if(o.allPages<=8 && o.allPages>=1){ 
					$.fn.draw(o,currval);	
				}else{
					if(currval>=1 && currval<=5){
						$.fn.draw(o,currval);	
					}else if(currval<o.allPages-4){
						$.fn.draw(o,currval,"1");
					}else{
						$.fn.draw(o,currval,"2");
					}
				}
				//向后台查询数据
				$.ajax({
					url:o.url,
					type:"post",
					data:$.extend({},o.data,{page:currval}),
					dataType:"json",
					error: function(){alert('请求超时，请稍候再试');},
					success: function(result){
						o.onResult(result.rows);
					} 
				});
				o.onChange(currval); 
			} 
		
		}); 
		
		//click a page
		_pageDiv.find('a').click(function(e){
			var currval = "";
			$this.empty();
			if($(this).attr("mark")=="shouye"){
				currval = 1;
			}else if($(this).attr("mark")=="moye"){
				currval = o.allPages;
			}else if($(this).attr("mark")=="next"){
				currval = parseInt(selectedpage)+1;
			}else{
				currval = $(this).html();
			}
			if(o.allPages<=8 && o.allPages>=1){ 
				$.fn.draw(o,currval);	
			}else{
				if(currval>=1 && currval<=5){
					$.fn.draw(o,currval);	
				}else if(currval<o.allPages-4){
					$.fn.draw(o,currval,"1");
				}else{
					$.fn.draw(o,currval,"2");
				}
			}
			
			//向后台查询数据
			$.ajax({
				url:o.url,
				type:"post",
				data:$.extend({},o.data,{page:currval}),
				dataType:"json",
				error: function(){alert('请求超时，请稍候再试');},
				success: function(result){
					o.onResult(result.rows);
				} 
			});
			o.onChange(currval);	
		});
	};
})(jQuery);